package com.senyint.hip.fhir.provider;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;
import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.r4.model.CodeSystem.CodeSystemContentMode;
import org.hl7.fhir.r4.model.CodeSystem.ConceptDefinitionComponent;
import org.hl7.fhir.r4.model.Enumerations.PublicationStatus;
import org.hl7.fhir.utilities.xhtml.NodeType;
import org.hl7.fhir.utilities.xhtml.XhtmlNode;
import org.springframework.stereotype.Component;

import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.server.IResourceProvider;

/**
 * *******description*******
 * 术语
 * *************************
 *
 * @author lcg
 * @date 2019/07/02 10:26
 */
@Component
public class CodeSystemProvider implements IResourceProvider {

    /**
     * 临时存储数据
     */
    private Map<String, CodeSystem> codeSystems = new HashMap<String, CodeSystem>();

    private long myNextId = 1;

    @PostConstruct
    public void init() {
        CodeSystem codeSystem = new CodeSystem();


        codeSystem.setId(new IdDt("CodeSystem","id123654"));
        Narrative narrative = new Narrative();
        narrative.setDiv(new XhtmlNode(NodeType.Element)
                .addText("婚姻状况代码表"));

        codeSystem.setText(narrative);

        codeSystem.setUrl("http://hl7china.org.cn/fhir/CodeSystem/maritalstatus");

        List<Identifier> theIdentifier = new ArrayList<Identifier>();
        Identifier i = new Identifier();
        i.setSystem("urn:ietf:rfc:3986");
        i.setValue("urn:oid:2.16.156.10011.2.3.3.5");
        theIdentifier.add(i);
        codeSystem.setIdentifier(theIdentifier);

        codeSystem.setName("婚姻状况代码表");
        codeSystem.setTitle("婚姻状况代码表");

        codeSystem.setStatus(PublicationStatus.ACTIVE);
        codeSystem.setContent(CodeSystemContentMode.COMPLETE);

        codeSystem.addConcept().setCode("10").setDisplay("未婚");
        codeSystem.addConcept().setCode("20").setDisplay("已婚");
        codeSystem.addConcept().setCode("21").setDisplay("初婚");
        codeSystem.addConcept().setCode("22").setDisplay("再婚");
        codeSystem.addConcept().setCode("23").setDisplay("复婚");
        codeSystem.addConcept().setCode("30").setDisplay("丧婚");
        codeSystem.addConcept().setCode("40").setDisplay("离婚");
        codeSystem.addConcept().setCode("90").setDisplay("未说明的婚姻状况");

        codeSystems.put("id123654", codeSystem);
    }

    @Override
    public Class<CodeSystem> getResourceType() {
        // TODO Auto-generated method stub
        return CodeSystem.class;
    }


    @Read
    public CodeSystem findCodeSystem(@IdParam IdType theId) {

        return codeSystems.get(theId.getIdPart());

    }


}
